package edu.purdue.cs.ds;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.math.BigInteger;
import java.util.ArrayList;

import edu.purdue.cs.ds.vss.Reconstructor;
import edu.purdue.cs.ds.vss.ShareStore;

/**
 * This will contact k out of n servers to obtain shares to reconstruct 
 * the secret
 * @author ruchith
 *
 */
public class BenignAdmin {

	public static void main(String[] args) throws Exception {
		
		//Read the k hosts from the hosts file
		String dirPath = args[0];
		int n = Integer.parseInt(args[1]);
		int k = Integer.parseInt(args[2]);
		File dir = new File(dirPath);
		String[] fileNames = dir.list();
		
		ArrayList<ShareStore> stores = new ArrayList<ShareStore>();
		String storeStr = "";
		for (int i = 0; i < fileNames.length; i++) {
			
			if(fileNames[i].equals(".svn") || fileNames[i].equals(".DS_Store")){
				continue;
			}
			
			File f = new File(dirPath + "/" + fileNames[i]);
			FileReader fr = new FileReader(f);
			BufferedReader reader = new BufferedReader(fr);
			String tmp = null;
			while((tmp = reader.readLine()) != null) {
				storeStr += tmp.trim() + "\n";
			}
			
			//System.out.println(storeStr);
			ShareStore tmpShareStore = new ShareStore(storeStr);
			System.out.println(tmpShareStore.getShares().size());
			stores.add(tmpShareStore);
			storeStr = "";
		}
		
		
		Reconstructor reconstructor = new Reconstructor();
		BigInteger reconstructedSecret = reconstructor.reconstructSecretFromStores(stores, n, k);
		System.out.println(reconstructedSecret);
	
	}
	
}
